package com.example.projectdemo.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.projectdemo.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeActivity extends AppCompatActivity {
    //把每个activity转成class
    private static class DemoInfo {
        private final Class<? extends android.app.Activity> demoClass;

        public DemoInfo(Class<? extends android.app.Activity> demoClass) {
            this.demoClass = demoClass;
        }
    }

    //把每个activity转成xxx.class
    private static final DemoInfo[] demos = {
            new DemoInfo(TeaActivity.class),
            new DemoInfo(ChafingActivity.class),
            new DemoInfo(Register.class)
    };

    private List<Map<String, Object>> lists;
    private SimpleAdapter adapter;
    private ListView listView;

    private String[] theme = {"一盒堂", "小火锅", "小co"};
    private int[] imageViews = {R.drawable.img,R.drawable.img_2,R.drawable.img_3};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        //需要把图片和文字(一个单元中的东西)用Map对应起来，必须这样做，这是下面要用到的适配器的一个参数
        lists = new ArrayList<>();
        for (int i = 0; i < theme.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("image", imageViews[i]);
            map.put("theme", theme[i]);
            lists.add(map);
        }

        //适配器指定应用自己定义的xml格式
        adapter = new SimpleAdapter(HomeActivity.this, lists, R.layout.form, new String[]{"image", "theme"}, new int[]{R.id.image1, R.id.text1});
        listView = (ListView) findViewById(R.id.mlistview);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HomeActivity.this, demos[position].demoClass);
                startActivity(intent);
            }
        });
    }
}